﻿using DelonixRegia.Classes;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DelonixRegia
{
    public partial class SearchStaff : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Account a = (Account)Session["acc"];

            if (a != null)
            {
                if (a.userType < 2)
                    Response.Redirect("Default.aspx");
            }
            else
                Response.Redirect("SignIn.aspx");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbxSearchStaffID.Text))
            {
                Classes.Staff s = StaffDBManager.SearchStaffByID(Convert.ToInt32(tbxSearchStaffID.Text));

                if (s != null)
                {
                    lblFirstName.Text = s.firstName.ToString();
                    lblLastName.Text = s.lastName.ToString();
                    lblNRIC.Text = s.nric.ToString();
                    lblDOB.Text = s.dateOfBirth.ToString("dd-MM-yyyy");
                    lblBankNo.Text = s.bankAccNo.ToString();
                    lblHomeAddress.Text = s.address.ToString();
                    lblPhoneNo.Text = s.phoneNo.ToString();
                    lblEmail.Text = s.emailAddress.ToString();

                    string duty = s.dutyType.ToString();
                    if (duty == "1")
                    {
                        duty = "General Maintenance";
                    }
                    else if (duty == "2")
                    {
                        duty = "Room Maintenance";
                    }
                    else if (duty == "3")
                    {
                        duty = "Estate Maintenance";
                    }
                    else
                    {
                        duty = "Security Maintenance";
                    }

                    lblDutyType.Text = duty;

                    pnlFirstStep.Visible = false;
                    pnlSecondStep.Visible = true;
                    Page.ClientScript.RegisterStartupScript(GetType(), "transitionToSecondStep", "transitionToSecondStep();", true);
                }
                else
                {
                    ClientScriptMgr.showErrorBox("The staff ID you entered is not found or does not exist!");
                }
            }
            else
            {
                ClientScriptMgr.showErrorBox("Please enter the staff ID you would like to search!");
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Classes.Staff s = StaffDBManager.SearchStaffByID(Convert.ToInt32(tbxSearchStaffID.Text));

            tbxFirstName.Text = s.firstName.ToString();
            tbxLastName.Text = s.lastName.ToString();
            tbxNRIC.Text = s.nric.ToString();
            tbxDOB.Text = s.dateOfBirth.ToString("dd-MM-yyyy");
            tbxBankNo.Text = s.bankAccNo.ToString();
            tbxAddress.Text = s.address.ToString();
            tbxPhoneNo.Text = s.phoneNo.ToString();
            tbxEmail.Text = s.emailAddress.ToString();
            ddlDutyType.SelectedValue = s.dutyType.ToString();

            pnlSecondStep.Visible = false;
            pnlThirdStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToThirdStep", "transitionToThirdStep();", true);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(tbxFirstName.Text)) && (!string.IsNullOrEmpty(tbxLastName.Text))
               && (!string.IsNullOrEmpty(tbxNRIC.Text)) && (!string.IsNullOrEmpty(tbxDOB.Text))
               && (!string.IsNullOrEmpty(tbxBankNo.Text)) && (!string.IsNullOrEmpty(tbxAddress.Text))
               && (!string.IsNullOrEmpty(tbxPhoneNo.Text)) && (!string.IsNullOrEmpty(tbxEmail.Text)))
            {
                if (ValidateDate(tbxDOB.Text))
                {
                    Staff s = new Staff();
                    s.staffID = Convert.ToInt32(tbxSearchStaffID.Text);
                    s.firstName = tbxFirstName.Text;
                    s.lastName = tbxLastName.Text;
                    s.nric = tbxNRIC.Text;
                    s.dateOfBirth = Convert.ToDateTime(tbxDOB.Text);
                    s.bankAccNo = tbxBankNo.Text;
                    s.address = tbxAddress.Text;
                    s.phoneNo = tbxPhoneNo.Text;
                    s.emailAddress = tbxEmail.Text;
                    s.dutyType = Convert.ToInt32(ddlDutyType.SelectedValue);

                    if (StaffDBManager.EditStaff(s) == 1)
                    {
                        ClientScriptMgr.showSuccessBox("Staff information has been successfully updated!");
                    }
                    else
                    {
                        ClientScriptMgr.Execute("$('#searchErrorBox').html('<strong>Error:</strong> Unable to save staff information, please try again later.');$('#searchErrorBox').hide().show();");
                    }
                }
                else
                {
                    ClientScriptMgr.Execute("$('#searchErrorBox').html('<strong>Error:</strong> Invalid date of birth! Please use this format: 31-05-1967');$('#searchErrorBox').hide().show();");
                }
            }
            else
            {
                ClientScriptMgr.Execute("$('#searchErrorBox').html('<strong>Error:</strong> One or more required fields are empty!');$('#searchErrorBox').hide().show();");
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!StaffDBManager.checkStaffWithShift(Convert.ToInt32(tbxSearchStaffID.Text)))
            {
                if (StaffDBManager.DeleteStaff(Convert.ToInt32(tbxSearchStaffID.Text)) == 1)
                {
                    tbxSearchStaffID.Text = "";
                    pnlThirdStep.Visible = false;
                    pnlFirstStep.Visible = true;
                    Page.ClientScript.RegisterStartupScript(GetType(), "transitionToFirstStep", "transitionToFirstStep();", true);
                    ClientScriptMgr.Execute("$('#deleteSuccessBox').html('<strong>Success:</strong> The staff information has been successfully deleted!');$('#deleteSuccessBox').hide().show();");
                }
                else
                {
                    ClientScriptMgr.Execute("$('#deleteErrorBox').html('<strong>Error:</strong> Unable to delete the staff information, please try again later.');$('#deleteErrorBox').hide().show();");
                }
            }
            else
            {
                ClientScriptMgr.Execute("$('#deleteErrorBox').html('<strong>Error:</strong> Unable to delete the staff information, staff still has working shift!');$('#deleteErrorBox').hide().show();");
            }
        }

        protected void btnGoBackPanel2_Click(object sender, EventArgs e)
        {
            tbxSearchStaffID.Text = "";
            pnlSecondStep.Visible = false;
            pnlFirstStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToFirstStep", "transitionToFirstStep();", true);
        }

        protected void btnGoBackPanel3_Click(object sender, EventArgs e)
        {
            tbxSearchStaffID.Text = "";
            pnlThirdStep.Visible = false;
            pnlFirstStep.Visible = true;
            Page.ClientScript.RegisterStartupScript(GetType(), "transitionToFirstStep", "transitionToFirstStep();", true);
        }

        private bool ValidateDate(string stringDateValue)
        {
            try
            {
                CultureInfo CultureInfoDateCulture = new CultureInfo("fr-FR");
                DateTime d = DateTime.ParseExact(stringDateValue, "dd-MM-yyyy", CultureInfoDateCulture);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}